package ru.papergames.web.blines.battle.model;

import javolution.util.FastMap;
import ru.papergames.common.beans.BattleMember;
import ru.papergames.common.form.NpcPlayerForm;
import ru.papergames.common.form.PcPlayerForm;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Alexander Troshanin
 */
public class BattleOrder2 {

    private static int idSequence = 1;

    private static synchronized int idSequence() {
        return idSequence++;
    }

    private final int id;

    private final int teamsCount;

    private FastMap<Long, BattleMember> pcMembers = new FastMap<Long, BattleMember>();

    private FastMap<Long, BattleMember> npcMembers = new FastMap<Long, BattleMember>();

    private List<FastMap<Long, BattleMember>> teams;

    public BattleOrder2(int teamsCount) {
        id = idSequence();
        this.teamsCount = teamsCount;
        teams = new ArrayList<FastMap<Long, BattleMember>>(teamsCount);
        for (int i=0; i<teamsCount; i++) {
            teams.add(new FastMap<Long, BattleMember>());
        }
    }

    public int getId() {
        return id;
    }

    public int getTeamsCount() {
        return teamsCount;
    }

    public FastMap<Long, BattleMember> getPcMembers() {
        return pcMembers;
    }

    public FastMap<Long, BattleMember> getNpcMembers() {
        return npcMembers;
    }

    public List<FastMap<Long, BattleMember>> getTeams() {
        return teams;
    }

    public boolean addBattlePcMember(PcPlayerForm player, int teamNum) {
        BattleMember existingMember = pcMembers.get(player.getId());
        if (existingMember != null) {
            return false;
        }
        if (teamNum >= teamsCount || teamNum < 0) {
            return false;
        }
        FastMap<Long, BattleMember> team = teams.get(teamNum);
/*
        if (team.size() == maxFightersCount) {
            return false;
        }
*/
        BattleMember member = new BattleMember(teamNum, player);
        team.put(member.getId(), member);
        pcMembers.put(member.getId(), member);
        return true;
    }

    public boolean addBattleNpcMember(NpcPlayerForm player, int teamNum) {
        BattleMember existingMember = npcMembers.get(player.getId());
        if (existingMember != null) {
            return false;
        }
        if (teamNum >= teamsCount || teamNum < 0) {
            return false;
        }
        FastMap<Long, BattleMember> team = teams.get(teamNum);
        BattleMember member = new BattleMember(teamNum, player);
        team.put(member.getId(), member);
        npcMembers.put(member.getId(), member);
        return true;
    }
}
